package com.dqp.cslggroup.Library;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.BaseActivity;
import com.dqp.cslggroup.Util.UiUtils;
import com.dqp.cslggroup.Web.myWeb;

import java.io.Serializable;
import java.util.Map;
import java.util.Objects;

public class lib_main extends BaseActivity {
    private String name, max, card, num, chao, dqjy, book, use_url;
    private String daili_url;
    private TextView sname, jie, yue, tuo, zjf, kyjf;
    private Map<String, String> cookie = null;
    private Button cq, wt, yy, pm, dq, xjbtn;
    private int select = 0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lib_main);
        Toolbar toolbar = findViewById(R.id.include_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        chao = intent.getStringExtra("chao");
        card = intent.getStringExtra("card");
        max = intent.getStringExtra("max");
        num = intent.getStringExtra("num");
        dqjy = intent.getStringExtra("dqjy");
        book = intent.getStringExtra("book");
        cookie = (Map<String, String>) intent.getSerializableExtra("cookies");
        daili_url = intent.getStringExtra("url");
        selectUrl();
        initCmp();
        setView();

    }

    private void selectUrl() {
        if (select == 1) {
            use_url = "http://10.28.60.4:8080";
        } else use_url = daili_url;
    }

    private void initCmp() {
        sname = findViewById(R.id.lib_main_name);
        jie = findViewById(R.id.lib_main_max_jie);
        yue = findViewById(R.id.lib_main_max_yuyue);
        tuo = findViewById(R.id.lib_main_max_weito);
        zjf = findViewById(R.id.lib_main_max_zjf);
        kyjf = findViewById(R.id.lib_main_max_kyjf);
        cq = findViewById(R.id.lib_chao);
        wt = findViewById(R.id.lib_weituo);
        yy = findViewById(R.id.lib_yuyue);
        dq = findViewById(R.id.lib_dqjy);
        pm = findViewById(R.id.lib_paiming);
        xjbtn = findViewById(R.id.lib_xj);
        dq.setOnClickListener(v -> {
            final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(lib_main.this);
            @SuppressLint("InflateParams") View view = LayoutInflater.from(lib_main.this).inflate(R.layout.book_list, null);
            builder.setView(view);
            Dialog dialog = builder.show();
            UiUtils.setDialogWindowAttr(dialog, lib_main.this);
            Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(android.R.color.transparent);
            ListView book_list = view.findViewById(R.id.book_dqjy);
            String[] book_Ary;
            if (book.length() > 0) {
                book_Ary = book.trim().split("无 {2}");
                ArrayAdapter arrayAdapter = new ArrayAdapter<>(lib_main.this, android.R.layout.test_list_item, book_Ary);
                book_list.setAdapter(arrayAdapter);
            }
            if (book.length() == 0) {
                book_Ary = new String[1];
                book_Ary[0] = "当前未借阅！";
                ArrayAdapter arrayAdapter = new ArrayAdapter<>(lib_main.this, android.R.layout.test_list_item, book_Ary);
                book_list.setAdapter(arrayAdapter);
            }

        });

        xjbtn.setOnClickListener(view -> {
            Intent intent = new Intent(lib_main.this, book_xj.class);
            intent.putExtra("cookies", (Serializable) cookie);
            intent.putExtra("url", use_url);
            intent.setClass(lib_main.this, book_xj.class);
            startActivity(intent);
        });


    }

    @SuppressLint("SetTextI18n")
    public void setView() {
        String Email;
        String[] card_Arry, chao_Arry, max_Arry, dq_Arry;
        if (card.length() != 0) {
            card_Arry = card.trim().split(" ");
            String card_start = "证件开始日期：" + card_Arry[0] + "\n";
            String card_end = "证件结束日期：" + card_Arry[1] + "\n";
            if (card_Arry.length >= 3) {
                Email = "Email：" + card_Arry[2];
            } else Email = "Email:未验证";
            name = "姓名：" + name + "\n" + card_start + card_end + Email;
            sname.setText(name);
        }
        if (max.length() > 0) {
            max_Arry = max.trim().split(" ");
            if (max_Arry.length >= 5) {
                jie.setText("最多可借\n" + max_Arry[0]);
                yue.setText("最多可预约\n" + max_Arry[1]);
                tuo.setText("最多可委托\n" + max_Arry[2]);
                zjf.setText("总积分\n" + max_Arry[3]);
                kyjf.setText("可用积分\n" + max_Arry[4]);
            }
        }
        if (chao.length() > 0) {
            chao_Arry = chao.trim().split(" ");
            if (chao_Arry.length >= 3) {
                cq.setText("超期图书\n" + chao_Arry[0]);
                wt.setText("委托到书\n" + chao_Arry[1]);
                yy.setText("预约到书\n" + chao_Arry[2]);
            }
        }
        if (num.length() > 0) {
            pm.setText("在所有读者中排在" + num + "的人之前");
        }
        if (dqjy.length() > 0) {
            dq_Arry = dqjy.trim().split(" ");
            dq.setText("当前借阅\n" + dq_Arry[0]);
        } else dq.setText("当前借阅\n0");
    }

    public void tiaowan(View view) {
        Intent intent = new Intent(lib_main.this, myWeb.class);
        intent.putExtra("url", "http://libwx.cslg.edu.cn/m/opac/advanced_search.action");
        startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            super.onDestroy();
            this.finish();
        }
    }


}
